package com.player.thirdGroup.controller;

import com.player.thirdGroup.entity.Role;
import com.player.thirdGroup.entity.User;
import com.player.thirdGroup.service.RoleService;
import com.player.thirdGroup.service.UserService;
import com.player.thirdGroup.util.BootStrapPage;
import com.player.thirdGroup.util.BootStrapTablePage;
import com.player.thirdGroup.util.IdWorker;
import com.player.thirdGroup.util.LoadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @author VULCAN
 * @create 2019-12-28 7:49
 * @desc 账号
 **/
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/admin")
    public String admin(){

        return "login";
    }

    //跳转页面
    @RequestMapping(value = "/admins")
    public String admins(){

        return "tree";
    }

    //跳转列表页面
    @RequestMapping(value = "/userlisthtml")
    public String userlisthtml(){

        return "userlist";
    }
    //列表
    @GetMapping("userlist")
    @ResponseBody
    public BootStrapPage<User> findAll(BootStrapTablePage page){
        Page<User> all = userService.findAll(page);
        return new BootStrapPage<User>(all.getContent(),all.getTotalElements());
    }
    //跳转页面
    @RequestMapping(value = "/zhuce")
    public String zhuce(Model model){
        List<Role> all = roleService.findAll();
        model.addAttribute("list",all);
        return "zhuce";
    }
    //角色列表
    @GetMapping("rlist")
    @ResponseBody
    public List<Role> rlist(Model model){
        List<Role> all = roleService.findAll();
        return all;
    }
    //跳转到注册页面
    @RequestMapping(value = "tozhuce")
    public String tozhuce(){
        return "zhuce";
    }
    /**
     * 注册
     * @param username
     * @param userimg
     * @param userpwd
     * @param userphone
     * @return
     */
    @RequestMapping(value = "adduser",method = RequestMethod.POST)
    public String zhuce(String username,
                        MultipartFile  userimg,
                        String userpwd,
                       String  userphone){
        System.err.println(username+"---------------");
        User user = new User();
        user.setUsername(username);
        user.setUserpwd(userpwd);
        user.setUsercdate(new Date());
        user.setUserphone(userphone);
        user.setUserstatus("0");
        //上传到指定位置
        String s = LoadUtil.ScPhoto(userimg);
        String uimg="/img/"+userimg.getOriginalFilename();
        user.setUserimg(uimg);
        userService.zhuce(user);
        return "login";
    }

    //获取验证码
    @RequestMapping(value = "getYzm")
    @ResponseBody
    public String getYzm(){
        String yzm = "";
        for(int i=0;i<6;i++) {
            int int1 = new Random().nextInt(10);
            yzm+=int1;
        }
        return yzm;
    }

    /**
     * 跳转到找回密码页面
     * @return
     */
    @RequestMapping(value = "togetPwd")
    public String togetPwd(){
        return "getPwd";
    }
    //忘记密码
    @RequestMapping(value = "forgetPwd",method = RequestMethod.POST)
    //@ResponseBody
    public String forgetPwd(@RequestParam(value = "userphone")String userphone,
                            @RequestParam(value = "userpwd")String userpwd){
        //根据手机号获取用户
        User user=userService.getUserByPhone(userphone);
        user.setUserpwd(userpwd);
        //修改密码
        userService.update(user);

        return "login";
    }
    //禁用and启用
    @GetMapping("jinAndqi")
    @ResponseBody
    public String jinAndqi(String userid,String userstatus){
        userService.updateStatus(userid,userstatus);
        return "修改状态成功";
    }

    /**
     * 编辑
     * @param userid
     * @param model
     * @return
     */
    @GetMapping("bianji")
    public String findByUser(String userid,Model model){
        //回显数据
       User user= userService.findByuser(userid);
       model.addAttribute("user",user);
       return "userUpdate";
    }
    /** 
     * @MethodName: NewUser 新建
     * @Description: TODO
     * @Param: [username, userimg, userpwd, userphone]
     * @Return: java.lang.String
     * @Author: ForeverLoveT
     * @Date: 8:38
    **/
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String NewUser(String username,
                        MultipartFile  userimg,
                        String userpwd,
                        String  userphone){
        System.err.println(username+"---------------");
        User user = new User();
        user.setUsername(username);
        user.setUserpwd(userpwd);
        user.setUsercdate(new Date());
        user.setUserid(UUID.randomUUID().toString());
        user.setUserphone(userphone);
        user.setUserstatus("0");
        //上传到指定位置
        String s = LoadUtil.ScPhoto(userimg);
        String uimg="/img/"+userimg.getOriginalFilename();
        user.setUserimg(uimg);
        userService.zhuce(user);
        return "userlisthtml";
    }

}
